/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hello;

import java.io.IOException;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Image;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.Screen;
import javax.microedition.lcdui.TextField;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotFoundException;
/**
 * @author Arthur
 */
public class AgendaMidlet extends MIDlet implements CommandListener{
    private Display display;        //display
    private Form incluirForm;       //formulario que inclue contatos
    
    private List agendaList;        //lista de opcoes da agenda
    private List contatoLista;
    
    private TextField nomeContato;     
    private TextField foneContato;     
    private TextField celularContato;
    private TextField emailContato;
    
    private Command cmdSair;        //sair do midlet
    private Command cmdNext;        //selecionar a opcao
    private Command cmdCancel;      //para poder voltar na tela anterior
    private Command cmdAdd;         //comando para add um contato
    
    private Contato contato;
    private Contato contato1;
    private ContatoDAO contatoDAO;
    
    String[] stringElements = {"Incluir Contatos","Alterar Contatos", "Excluir Contatos","Pesquisar Contatos"};
    Imagens a = new Imagens();
    Image[] imageElements = { a.loadImage("/hello/incluir.png"),a.loadImage("/hello/alterar.png"), 
        a.loadImage("/hello/excluir.png"), a.loadImage("/hello/pesquisar.png") };
   
    

    public void startApp() {
        display = Display.getDisplay(this);
               
        agendaList = new List("Agenda de Contatos", List.IMPLICIT, stringElements, imageElements);
        //agendaList.setFont(10, null); 
        cmdSair = new Command("Sair", Command.EXIT, 0);
        cmdNext = new Command("Selecionar", Command.SCREEN, 0);
        agendaList.addCommand(cmdSair);
        agendaList.addCommand(cmdNext);
        agendaList.setCommandListener(this);
                
        display.setCurrent(new SplashScreen(display, agendaList));
        
        //Abrir RMS para utlizacao!!
        contatoDAO = new ContatoDAO();
        try {
            contatoDAO.openRMS();
        } catch (RecordStoreNotFoundException ex) {
            ex.printStackTrace();
        } catch (RecordStoreFullException ex) {
            ex.printStackTrace();
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }
        
    }
    
    public void pauseApp() {
    }
    
    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable d) {
        if (d == agendaList) {
            if (c == cmdSair) {
                    destroyApp(true);
                    notifyDestroyed();
            }else if(c == List.SELECT_COMMAND || c == cmdNext){
                switch (agendaList.getSelectedIndex()){
                    case 0:
                        System.out.println("Incluir Contatos");
                        incluirScreen();
                        //alert.setTitle("Ainda nao implementado");
                        break;
                    case 1:
                        System.out.println("Alterar Contatos");
                        excluirScreen();
                        break;
                    case 2:
                        System.out.println("Excluir Contatos");
                        excluirScreen();
                        //alert.setTitle("Ainda nao implementado");
                        break;
                    case 3:
                        System.out.println("Pesquisar");
                        break;
                        //alert.setTitle("Ainda nao implementado");
                    default:
                        System.out.println("Indice Invalido");
                        break;
                        
                }
            }
        }
        else if (d==incluirForm){
            if(c==cmdCancel)
                display.setCurrent(agendaList);
            else if(c==cmdAdd)
                try {
                    System.out.println(nomeContato.getString());
                    contatoDAO.incluirRMS(nomeContato.getString(),foneContato.getString(),
                            celularContato.getString(), emailContato.getString());
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                
                showMessage("Contato Inserido com Sucesso");
            
            }
            
    }
    
    private void incluirScreen(){
        if(incluirForm == null){
            
            incluirForm = new Form("Incluir Contato");
            cmdCancel = new Command("Cancelar", Command.BACK, 2);
            cmdAdd = new Command("Salvar", Command.OK, 1);
            incluirForm.addCommand(cmdAdd);
            incluirForm.addCommand(cmdCancel);
            incluirForm.setCommandListener(this);
            
            contato = new Contato();
            
            nomeContato = new TextField("Nome:", "", 50, TextField.ANY);
            foneContato = new TextField("Fone:","",8,TextField.PHONENUMBER);
            celularContato = new TextField("Celular: ", "", 8, TextField.PHONENUMBER);
            emailContato = new TextField("Email:", "", 50, TextField.ANY);
            
            
            incluirForm.append(nomeContato);
            incluirForm.append(foneContato);
            incluirForm.append(celularContato);
            incluirForm.append(emailContato);
        }
        
        nomeContato.delete(0, nomeContato.size());
        foneContato.delete(0, foneContato.size());
        celularContato.delete(0,celularContato.size());
        emailContato.delete(0, emailContato.size());
        
        contato.setCelular(celularContato.getString());
        contato.setEmail(emailContato.getString());
        contato.setFone(foneContato.getString());
        contato.setNome(nomeContato.getString());
        
        display.setCurrent(incluirForm);
        
         
    }
    
    private void excluirScreen(){
        
        
        Form excluirForm = new Form("Excluir Contato");
        contatoLista = contatoDAO.findRMS();
        //excluirForm.append(contatoLista);
        display.setCurrent(contatoLista);
        
    }
    
    private void showMessage(String msg) {

             Alert alert = new Alert("Mensagem", msg, null, 
                AlertType.CONFIRMATION);
             display.setCurrent(alert,agendaList);

       }
    
}
